package com.ruoyi.partybuilding.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.partybuilding.common.EQueryHelper;
import com.ruoyi.partybuilding.domain.vo.MeetingStatVo;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.partybuilding.mapper.MeetingInfoMapper;
import com.ruoyi.partybuilding.domain.MeetingInfo;
import com.ruoyi.partybuilding.service.IMeetingInfoService;

import javax.annotation.Resource;

/**
 * 会议信息Service业务层处理
 * 
 * @author he-liangliang@outlook.com
 * @date 2024-11-27
 */
@Service
public class MeetingInfoServiceImpl implements IMeetingInfoService 
{
    @Autowired
    private MeetingInfoMapper meetingInfoMapper;

    @Resource
    EQueryHelper queryHelper;

    /**
     * @return
     */
    public List<MeetingStatVo> MeetingStat(Long deptId, Integer year,Long meetingWay) {
        var caseSql=",sum(case when MONTH(start_time)=%1$s then 1 else 0 end)   m%1$scount";
        var stb=new StringBuilder();
        for (int i = 1; i <13 ; i++) {
            stb.append(String.format(caseSql,i));
        }
        var statSql="select max(YEAR(start_time)) `year`,max(MONTH(start_time)) `month`,max(title) title,COUNT(1) total,dept_id,max(dept_name) dept_name,type_code %1$s from pb_meeting_info where 1=1 %2$s GROUP BY dept_id,type_code,YEAR(start_time) ORDER BY YEAR(start_time) ";

        var whereSql="";
        if(deptId!=null)
            whereSql+=" and dept_id="+deptId;
        if(year!=null)
            whereSql+=" and YEAR(start_time)="+year;
        if(meetingWay!=null)
            whereSql+=" and meeting_way="+meetingWay;
        var list=queryHelper.CreateItems(String.format(statSql,stb.toString(),whereSql)).resultPkg().sqlQuery(MeetingStatVo.class);
        return list;
    }

    /**
     * 查询会议信息
     * 
     * @param id 会议信息主键
     * @return 会议信息
     */
    @Override
    public MeetingInfo selectMeetingInfoById(Long id)
    {
        return meetingInfoMapper.selectMeetingInfoById(id);
    }

    /**
     * 查询会议信息列表
     * 
     * @param meetingInfo 会议信息
     * @return 会议信息
     */
    @Override
    public List<MeetingInfo> selectMeetingInfoList(MeetingInfo meetingInfo)
    {
        return meetingInfoMapper.selectMeetingInfoList(meetingInfo);
    }

    /**
     * 新增会议信息
     * 
     * @param meetingInfo 会议信息
     * @return 结果
     */
    @Override
    public int insertMeetingInfo(MeetingInfo meetingInfo)
    {
        meetingInfo.setCreateTime(DateUtils.getNowDate());
        return meetingInfoMapper.insertMeetingInfo(meetingInfo);
    }

    /**
     * 修改会议信息
     * 
     * @param meetingInfo 会议信息
     * @return 结果
     */
    @Override
    public int updateMeetingInfo(MeetingInfo meetingInfo)
    {
        return meetingInfoMapper.updateMeetingInfo(meetingInfo);
    }

    /**
     * 批量删除会议信息
     * 
     * @param ids 需要删除的会议信息主键
     * @return 结果
     */
    @Override
    public int deleteMeetingInfoByIds(Long[] ids)
    {
        return meetingInfoMapper.deleteMeetingInfoByIds(ids);
    }

    /**
     * 删除会议信息信息
     * 
     * @param id 会议信息主键
     * @return 结果
     */
    @Override
    public int deleteMeetingInfoById(Long id)
    {
        return meetingInfoMapper.deleteMeetingInfoById(id);
    }
}
